﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Collections;
using ECCIT;
using ECCIT.Web;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Globalization;
using System.IO;
public partial class admin_UKNOW_CnEnMapping_Add : System.Web.UI.Page
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_CN_ID = "Cid";
    private const string VIEWSTATE_EN_ID = "enid";
    private const string VIEWSTATE_TYPE_ID = "typeid";
    private const string VIEWSTATE_MAIN_ID = "mainID";
    string sname = "";
    string tname = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];
            ViewState[VIEWSTATE_TYPE_ID] = Request.Params[VIEWSTATE_TYPE_ID];

            int typeID = Convert.ToInt32(Request.QueryString["typeid"]);//用于修改页映射时,初始化页面获得实体类型
            var CnID = Request.QueryString["Cid"] != null ? int.Parse(Request.QueryString["Cid"]) : 0;
            var EnID = Request.QueryString["enid"] != null ? int.Parse(Request.QueryString["enid"]) : 0;
            ViewState[VIEWSTATE_TYPE_ID] = typeID;
            InitPage(CnID, EnID, typeID);
        }
    }

    private void InitPage(int CnID, int EnID, int typeID)
    {
        BindSourceDropDownListItems(typeID);
        BindDestDropDownListItems(typeID);

        CnEnMappingMod model = null;
        if (CnID > 0)
        {
            CnENnMappBLL bll = new CnENnMappBLL();
            //model = bll.GetCnEnAppModel(typeID, CnID);
            model = bll.GetCnEnAppModel(typeID, CnID, EnID);
            ViewState[VIEWSTATE_MAIN_ID] = model.Col_mapping_id;
            BindModelToControls(model);
        }
    }
    private void BindDropDownListItems(DropDownList ddl, IList<KeyValuePair<int, string>> models)
    {
        ddl.Items.Clear();
        if (models == null) return;

        foreach (KeyValuePair<int, string> model in models)
            ddl.Items.Add(new ListItem(model.Key + "." + model.Value, model.Key.ToString()));
    }

    private void BindSourceDropDownListItems(int typeID)
    {
        ICnEnMapping mapping = CnEnMappingFactory.CreateMappingBLL((int)LanguageType.cn, typeID);
        IList<KeyValuePair<int, string>> models = mapping.RetrieveDropDownListItems();
        BindDropDownListItems(SourceEventName, models);
    }

    private void BindDestDropDownListItems(int typeID)
    {
        ICnEnMapping mapping = CnEnMappingFactory.CreateMappingBLL((int)LanguageType.en, typeID);
        IList<KeyValuePair<int, string>> models = mapping.RetrieveDropDownListItems();
        BindDropDownListItems(ToEventName, models);
    }

    private void BindModelToControls(CnEnMappingMod model)
    {
        if (model == null) return;
        if (SourceEventName.Items.FindByValue(model.Col_data_id_cn.ToString()) != null)
            SourceEventName.SelectedValue = model.Col_data_id_cn.ToString();

        if (ToEventName.Items.FindByValue(model.Col_data_id_en.ToString()) != null)
            ToEventName.SelectedValue = model.Col_data_id_en.ToString();
    }

    protected void btnSync_click(object sender, EventArgs e)
    {
        int ID = int.Parse(Request.QueryString["Cid"] != null ? Request.QueryString["Cid"] : "0");
        int Cid = Convert.ToInt32(SourceEventName.SelectedValue);
        int Eid = Convert.ToInt32(ToEventName.SelectedValue);
        int typeID = Convert.ToInt32(Request.QueryString["typeid"]);
        
        CnENnMappBLL bll = new CnENnMappBLL();
        
        int mainID = 0;
        if (ViewState[VIEWSTATE_MAIN_ID] != null)
            mainID = int.Parse(ViewState[VIEWSTATE_MAIN_ID].ToString());

        CnEnMappingMod info = new CnEnMappingMod();
        info.Col_data_id_cn = Cid;
        info.Col_data_id_en = Eid;
        info.Col_data_type = typeID;
        if (mainID > 0)
            info.Col_mapping_id = mainID;

        if (mainID > 0)
        {
            //如果是修改,则判断数据表中是否已经存在所要添加的主键ID,此表以Col_data_type和Col_data_id_cn作为联合主键;
            //调用,根据Col_data_type和Col_data_id_cn查找记录的方法,若返回值大于0,则已存在,提示;否则继续执行修改方法;
            if (bll.GetCnEnAppModel(typeID, Cid, Eid, mainID) != null)
            {
                //Response.Write("<script>alert('数据表中已存在类型为,值为的映射关系!')</script>");
                Response.Write("<script>alert('数据表中已存在类型为" + typeID + ", 中文数据ID值为 " + Cid + " 或者英文数据ID值为 " + Eid + " 的映射关系!')</script>");
                return;
            }
            //
            else
            {
                bool res = bll.Update(info);
                if (res)
                {
                    Response.Write("<script>alert('修改成功');window.close();</script>");
                }
                else
                {
                    Response.Write("<script>alert('修改失败')</script>");
                }
            }

           
        }
        else
        {
            //如果是新增,则判断数据表中是否已经存在所要添加的主键ID,此表以Col_data_type和Col_data_id_cn作为联合主键;
            //调用,根据Col_data_type和Col_data_id_cn查找记录的方法,若返回值大于0,则已存在,提示;否则继续执行插入方法;
            if (bll.GetCnEnAppModel(typeID, Cid, Eid,null) != null)
            {
                //Response.Write("<script>alert('数据表中已存在类型为,值为的映射关系!')</script>");
                Response.Write("<script>alert('数据表中已存在类型为" + typeID + ", 中文数据ID值为 " + Cid + " 或者英文数据ID值为 " + Eid + " 的映射关系!')</script>");
                return;
            }
            //
            else
            {
                bool res = bll.Add(info);
                if (res)
                {
                    Response.Write("<script>alert('添加成功');window.close(); </script>");
                }
                else
                {
                    Response.Write("<script>alert('添加失败')</script>");
                }
            }
        }
        //Response.Redirect("CnEnMapping.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&typeid=" + ViewState[VIEWSTATE_TYPE_ID]);
    }
}